<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
        <title>Document</title>
        <script>
            /* 
                1.编写一个函数，计算两个数字的和、差、积、商
                    要求：使用传参的形式
             */
            function operation(num1, operator, num2){
                //operator根据运算符的符号，进行不同的运算
                switch(operator){
                    case "+":
                        return num1 + num2;
                        break;
                    case "-":
                        return num1 - num2;
                        break;
                    case "*":
                        return num1 * num2;
                        break;
                    case "/":
                        return num1 / num2;
                        break;
                    default:
                        alert("error");
                        break;
                }
            }

           /*  var res = operation(10, "-", 5);
            alert(res); */

            // alert(operation(10, "+", 5));

            /* 
                3.编写一个函数，输入n为偶数时，调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数求/1+1/3+...+1/n 
             */
            
            function sum(n){
                var res = 0; //用于记录累加的和
                if(n % 2 == 0){
                    //偶数
                    for(var i = 2; i <= n; i += 2){
                        res += 1 / i;
                    }
                }else{
                    //奇数
                    for(var i = 1; i <= n; i += 2){
                        res += 1 / i;
                    }
                }
                return res;
            }

            // alert(sum(4));

            alert(sum(3));

        </script>
    </head>
    <body>
        
    </body>
</html>